package com.szholly.plug.activity.interceptor.defulat;

import java.util.Date;

import org.springframework.stereotype.Component;

import com.szholly.plug.activity.core.Constants;
import com.szholly.plug.activity.interceptor.BpmHandleOptionBase;
import com.szholly.plug.activity.interceptor.BpmInterceptorRegister;
import com.szholly.plug.activity.interceptor.OptionInfo;
import com.szholly.plug.activity.model.BpmConfigCache;
import com.szholly.plug.activity.model.BpmConfigRow;
import com.szholly.plug.activity.model.BpmOverWorklistRow;
import com.szholly.plug.activity.model.BpmOverWorklistTable;
import com.szholly.utils.util.StringUtils;
import com.szholly.data.general.GlobalDataType;
import com.szholly.data.general.IQueryMap;
import com.szholly.data.general.QueryMap;
import com.szholly.data.general.RowBase;

/**
 * 流程已办件
 */
@Component
public class OverWorklistBpmOption extends BpmHandleOptionBase {
	/**
	 * 注册拦截接口
	 */
	static {
		OverWorklistBpmOption interceptor = new OverWorklistBpmOption();
		BpmInterceptorRegister.RegDbInterceptor(interceptor);
	}

	/**
	 * 流程任务操作
	 * 
	 * @param optionInfo
	 */
	@Override
	public void taskOption(OptionInfo optionInfo) {
		if(optionInfo.TaskPDefinitionKey.equals("U0")){
			return;
		}
		saveWork((String)optionInfo.BusinessRow.getValue(GlobalDataType.String, Constants.FIELD_PROID) ,
				optionInfo.BpmId, optionInfo.TaskId, optionInfo.TaskDefinitionKey, optionInfo.TaskState, 
				optionInfo.BusinessRow, optionInfo);
	}

	/**
	 * 保存待办事项
	 * 
	 * @param pDefId
	 *            流程定义ID
	 * @param taskId
	 *            当前任务ID
	 * @param taskName
	 *            当前任务名称
	 * @param users
	 *            当前节点的审批人列表
	 * @param row
	 *            当前数据行
	 */
	private void saveWork(String exeId, String pDefId, String taskId, String taskDefinId,
			String taskName, RowBase row, OptionInfo optionInfo) {
		BpmOverWorklistRow work = null;
		IQueryMap queryMap = new QueryMap();
		queryMap.getWhereMap().put(BpmOverWorklistRow.FIELD_BPMKEY, exeId);
		work = BpmOverWorklistTable.getSingleRef().getEntity(BpmOverWorklistRow.class, queryMap);
		if (work == null) {
			work = new BpmOverWorklistRow();
		}
		work.setBpmKey(exeId);
		work.setBegintime(new Date());
		work.setITaskid(taskId);
		work.setTaskDefinid(taskDefinId);
		work.setSDefinid(pDefId);
		work.setSTaskname(taskName);
		work.setSAssingusers(optionInfo.TaskAssigneeUsers);
		if(optionInfo.TaskDefinitionKey=="U9"){
			work.setEndtime(new Date());
		}
		
		String key = (String) row.getValue(GlobalDataType.String,
				optionInfo.pTable.getGUIDFieldName());
		work.setKey(key);

		BpmConfigRow configRow = BpmConfigCache.getConfig(pDefId);
		if (configRow != null) {
			work.setSDefinname(configRow.getSDefitionName());
			work.setSFormurl(configRow.getSFormurl());
			work.setFormName(configRow.getFormName());
			work.setFormPopId(configRow.getFormPopId());
			work.setTableName(configRow.getTableName());
			work.setSGroup(configRow.getSGroup());
			String businessFields = configRow.getBusiness();
			if (!StringUtils.IsNullOrSpace(businessFields)) {
				String[] fields = businessFields.split(",");
				String business = "";
				for (String field : fields) {
					String value = (String) row.getValue(GlobalDataType.String,
							field);
					if (StringUtils.IsNullOrSpace(value)) {
						business += ",";
					} else {
						business += value + ",";
					}
				}
				business = business.substring(0, business.length() - 1);
				work.setBusiness(business);
			}
		} else {
			work.setSDefinname("");
			work.setSFormurl("");
			work.setSGroup("");
			work.setBusiness("");
		}
		BpmOverWorklistTable.getSingleRef().save(work);
	}
}